<template>
  <div class="app-container">
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="800px"
      :close-on-click-modal="false"
      :show-close="true"
      append-to-body
      :before-close="handleClose"
    >
      <div slot="title">
        {{ title }}
      </div>
      <div style="height: 350px">
        <!-- 搜索工具栏 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
          <el-form-item label="产品名称" prop="search">
            <el-input v-model="queryParams.search" placeholder="请输入产品名称"
                      clearable style="width: 240px" @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 表格 -->
        <el-table ref="customerTable" highlight-current-row :data="tableData" style="width: 100%"
                  @selection-change="handleSelectionChange" height="265"
        >
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column show-overflow-tooltip align="center" fixed prop="productName" label="产品名称"
                           min-width="135"
          >
            <template slot-scope="scope">
              <el-button type="text">{{ scope.row.productName }}</el-button>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="categoryName" label="产品类型" min-width="115"/>
          <el-table-column align="center" prop="unit" label="产品单位" min-width="90">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_crm_product_unit" :value="scope.row.unit"/>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip align="center" prop="productSn" label="产品编码" min-width="175"/>
          <el-table-column align="center" prop="price" label="价格" min-width="135">
            <template slot-scope="scope">
              <div style="color: #1ab394;font-weight: 600">{{ scope.row.price }}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="description" label="产品描述" min-width="175"/>
          <el-table-column align="center" prop="status" label="是否上下架" min-width="115">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_crm_grounding" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="ownerUserName" label="负责人" min-width="155"/>
          <el-table-column align="center" prop="createUserName" label="创建人" min-width="155"/>
        </el-table>
        <!-- 分页条 -->
        <pagination
          v-show="total>0"
          :total="total"
          :page-sizes="[15,30,50,100]"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.limit"
          @pagination="getList"
        />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="submitForm">保 存</el-button>
        <el-button size="small" @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { listByParamsForTable } from '@/api/crm/Product'

export default {
  name: 'ProductSelectPopover',
  dicts: ['sys_crm_grounding', 'sys_crm_product_unit', 'sys_crm_product_category'],
  components: {},
  props: {},
  data() {
    return {
      dialogVisible: false,
      title: '关联产品模块',
      tableData: [],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 15,
        search: ''
      },
      total: 0,
      selectData: null,
      productId: null
    }
  },
  // 在实例创建完成后被立即调用
  created() {

  },
  // 实例被挂在后调用
  mounted() {

  },
  // 方法
  methods: {
    // 查询数据
    getList() {
      // 查询数据
      listByParamsForTable(this.queryParams).then(res => {
        this.tableData = res.data.records
        this.total = res.data.totalCount
      })
    },
    // 查询
    handleQuery() {
      this.getList()
    },
    open() {
      this.dialogVisible = true
      // 查询数据
      this.getList()
    },
    handleClose() {
      this.reset()
      this.dialogVisible = false
    },
    // 重置
    resetQuery() {
      this.reset()
      this.getList()
    },
    reset() {
      this.queryParams = {
        page: 1,
        limit: 15,
        search: ''
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectData = selection
    },
    submitForm() {
      this.handleClose()
      this.$emit('callback', this.selectData)
    }
  }
}
</script>

<style scoped>

</style>
